﻿using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

public class BuffNode
{
    public int buffId;
    public BuffState state;
    public float freezeTime;
    public float durationTime;
    public float passedTime;

    public BuffNode(int buffId)
    {
        this.buffId = buffId;
        this.state = BuffState.Ready;
        this.freezeTime = -1;
        this.durationTime = -1;
        this.passedTime = 0;
    }
}
public struct BuffTimeLine
{
    
    private Dictionary<int, BuffNode> buffNodeSet; //玩家所拥有的所有buff
    public void Init()
    {
        buffNodeSet = new Dictionary<int, BuffNode>();
    }

    public bool HasBuff(int buffId)
    {
        if (!this.buffNodeSet.ContainsKey(buffId))
        {
            return false;
        }

        BuffNode node = this.buffNodeSet[buffId];

        if (node.state != BuffState.Started)
        {
            return false;
        }

        return true;
    }

    public bool StartBuff(int buffId)
    {
        Debug.Log("启用Buff：" + buffId);
        BuffNode node = this.AddBuffNode(buffId);
        if (node.state != BuffState.Ready)
        {
            return false; 
        }

        node.state = BuffState.Started;
        node.passedTime = 0;
        return true; 
    }

    public void OnUpdate(float dt)
    {
        if (buffNodeSet == null)
        {
            return;
        }
        foreach (var kvp in buffNodeSet)
        {
            BuffNode node = kvp.Value;
            if (node.state == BuffState.Started)
            {
                node.passedTime += dt;
                if (node.passedTime >= node.durationTime)
                {
                    node.state = BuffState.Freezed;
                    node.passedTime = 0;
                    // EventMgr.Instance.Emit(GM_EVent.UI, UIEvent.BuffFreezed);
                }
                
            }else if(node.state == BuffState.Freezed)
            {
                node.passedTime += dt;
                if (node.passedTime >= node.freezeTime)
                {
                    node.state = BuffState.Ready;
                    node.passedTime = 0;
                    // EventMgr.Instance.Emit(GM_EVent.UI, UIEvent.BuffReady);
                    Debug.Log("Buff失效：" + node.buffId);
                }
            }
        }
    }

    public BuffNode AddBuffNode(int buffId)
    {
        BuffNode node = null;
        if (!this.buffNodeSet.ContainsKey(buffId))
        {
            node = BuffManager.Instance.CreateBuffNode(buffId);
            this.buffNodeSet.Add(buffId, node);
        }
        else
        {
            node = this.buffNodeSet[buffId];
        }

        return node;
    }
    
    public void RemoveBuffNode(int buffId)
    {
        if (this.buffNodeSet.ContainsKey(buffId))
        {
            this.buffNodeSet[buffId].state = BuffState.Invalid;
            this.buffNodeSet.Remove(buffId);
        }
    }

    /// <summary>
    /// 计算buff的效果
    /// </summary>
    /// <param name="funcName">buff的效果方法名称</param>
    /// <param name="result">效果改变值</param>
    public void CalcAllBuffsWithFuncName(string funcName, FightCalcResult result)
    {
        foreach (BuffNode node in buffNodeSet.Values)
        {
            if (node.state == BuffState.Started)
            {
                MethodInfo m = BuffManager.Instance.GetProcesserFuncByBuffId(node.buffId, funcName);
                if (m != null)
                {
                    m.Invoke(null, new object[] { node.buffId, result });
                }
            }
        }
         
    }
}